package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.SuddenTimeHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.zsmart.core.log.ZSmartLogger;
import org.springframework.stereotype.Service;

/**
 * @author wanghl386
 * @version 1.0
 * @date 2021/5/26 11:32
 */
@Service
@AttrHandler(SuddenTimeHandler.class)
public class SuddenTimeLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(SuddenTimeLinkHandler.class);

    /**
     * 突发周期
     */
    private static final String SUDDEN_CYCLE = "200001824";
    private static final String SUDDEN_START = "210004306";
    private static final String SUDDEN_END = "210004307";

    /**
     * 周
     */
    private static final String WEEK = "100004331";

    /**
     * 月
     */
    private static final String MONTH = "100002333";

    /**
     * 年
     */
    private static final String YEAR = "100002481";

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        try {
            // 突发周期
            KiteAttr suddenCycle = inst.getAttr(SUDDEN_CYCLE);
            // 突发开始时间
            KiteAttr suddenStart = inst.getAttr(SUDDEN_START);
            // 突发结束时间
            KiteAttr suddenEnd = inst.getAttr(SUDDEN_END);

            if (!KiteStringUtils.isEmpty(suddenStart.getValue()) && !KiteStringUtils.isEmpty(suddenEnd.getValue())) {
                if (KiteStringUtils.equals(suddenCycle.getValue(), WEEK)) {
                    if (getWeek(suddenStart.getValueDesc()) > getWeek(suddenEnd.getValueDesc())) {
                        ErrorConsts.BUSI_COMMON_ERROR.throwOut("突发开始时间需要小于突发结束时间，请重新输入！");
                    }
                } else {
                    if (Integer.valueOf(suddenEnd.getValue()) < Integer.valueOf(suddenStart.getValue())) {
                        ErrorConsts.BUSI_COMMON_ERROR.throwOut("突发开始时间需要小于突发结束时间，请重新输入！");
                    }
                }
            }
        } catch (NumberFormatException e) {
            LOGGER.error("handle-->", e, e.getMessage());
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private int getWeek(String week) {
        switch (week) {
            case "星期一": {
                return 1;
            }
            case "星期二": {
                return 2;
            }
            case "星期三": {
                return 3;
            }
            case "星期四": {
                return 4;
            }
            case "星期五": {
                return 5;
            }
            case "星期六": {
                return 6;
            }
            default: {
                return 7;
            }
        }
    }
}
